from sqlalchemy import Column, Integer, String, DateTime, ForeignKey, Numeric, Enum, Text, Date
from sqlalchemy.orm import relationship
from sqlalchemy.sql import func
from app.core.database import Base
import enum

class ContractStatus(str, enum.Enum):
    UPLOADED = "uploaded"
    IN_PROGRESS = "in_progress"
    COMPLETED = "completed"

class Contract(Base):
    __tablename__ = "contracts"
    
    id = Column(Integer, primary_key=True, index=True)
    customer_id = Column(Integer, ForeignKey("customers.id"), nullable=False, index=True)
    contract_number = Column(String(50), unique=True, nullable=False, index=True)
    contract_date = Column(Date, nullable=False)
    contract_amount = Column(Numeric(12, 2), nullable=False)
    file_url = Column(String(500), nullable=True)
    status = Column(Enum(ContractStatus), default=ContractStatus.UPLOADED, nullable=False)
    notes = Column(Text, nullable=True)
    uploaded_by = Column(String(50), nullable=True)
    created_at = Column(DateTime(timezone=True), server_default=func.now())
    updated_at = Column(DateTime(timezone=True), onupdate=func.now())
    
    customer = relationship("Customer", back_populates="contracts")
    project = relationship("Project", back_populates="contract", uselist=False)

